<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <!-- 引入样式 -->
  <link rel="stylesheet" href="../../plugins/element-ui/index.css" />
  <link rel="stylesheet" href="../../styles/common.css" />
  <link rel="stylesheet" href="../../styles/page.css" />
  <style>
    #member-app  .notAdmin::after{
      border: 0 !important;

    }
  </style>
</head>
<body>
  <div class="dashboard-container" id="member-app">
    <div class="container">
      <div class="tableBar">
        <el-input
          v-model="input"
          placeholder="书名"
          style="width: 250px"
          clearable
           @keyup.enter.native="handleQuery"
        >
          <i
            slot="prefix"
            class="el-input__icon el-icon-search"
            style="cursor: pointer"
            @click="handleQuery"
          ></i>
        </el-input>

        <div class="tableLab">
          <el-button
                  type="danger"
                  @click="deleteBookHandle('批量', null)"
          >
            + 批量删除
          </el-button>
          <el-button
                  type="primary"
                  @click="addBookHandle('add')"
          >
            + 添加图书
          </el-button>
        </div>

      </div>
      <el-table
        :data="tableData"
        stripe
        class="tableBox"
        @selection-change="handleSelectionChange"
      >
        <el-table-column
        type="selection"
        width="25"
        ></el-table-column>
        <el-table-column
          prop="id"
          label="ID"
        ></el-table-column>
        <el-table-column prop="image" label="图片" align="center">
          <template slot-scope="{ row }">
            <el-image style="height: 40px; border:none;cursor: pointer;"
                      :src="getImage(row.image)"
                      :preview-src-list="[ `/common/download?name=${row.image}` ]" >
              <div slot="error" class="image-slot">
                <img src="./../../images/icons/btn_back@2x.png"  style="width: auto; height: 40px; border:none;" >
              </div>
            </el-image>
          </template>
        </el-table-column>
        <el-table-column
          prop="name"
          label="图书名称"
        ></el-table-column>
        <el-table-column
          prop="author"
          label="作者"
        ></el-table-column>
        <el-table-column
          prop="price"
          label="图书价格"
        ></el-table-column>
        <el-table-column
          prop="type"
          label="图书类型"
        ></el-table-column>
        <el-table-column
          prop="description"
          label="图书描述"
        ></el-table-column>
        <el-table-column
                prop="stock"
                label="图书库存"
        ></el-table-column>
       <!-- <el-table-column label="图书状态">
          <template slot-scope="scope">
            {{ String(scope.row.status) === '0' ? '未借出' : '已借出' }}
          </template>
        </el-table-column>-->
        <el-table-column
          label="操作"
          width="160"
          align="center"
        >
          <template slot-scope="scope">
            <el-button
              type="text"
              size="small"
              class="blueBug"
              @click="notHotBookHandle(scope.row.id)"
              :class="{notAdmin:user !== 'admin'}"
            >
              取消推荐
            </el-button>


          </template>
        </el-table-column>
      </el-table>

      <el-dialog title="借阅图书"
                 :visible.sync="dialogFormVisible"
                 :modal = false
      >

          <el-form ref="form" :model="form" label-width="80px">

            <el-form-item label="图书名称:" prop="name">
              <el-input v-model="ruleForm.name" placeholder="请输入图书名称" maxlength="20"/>
            </el-form-item>

            <el-form-item label="图书作者:" prop="author">
              <el-input v-model="ruleForm.author" placeholder="请输入图书作者" maxlength="20"/>
            </el-form-item>

            <el-form-item label="图书价格:" prop="price">
              <el-input v-model="ruleForm.price" placeholder="请输入图书价格" maxlength="20"/>
            </el-form-item>
            <el-form-item label="图书类型:" prop="typeId">
              <el-input v-model="ruleForm.type" placeholder="请输入图书类型" maxlength="20"/>
              </el-select>
            </el-form-item>
            <el-form-item label="图书详情:" prop="description">
              <el-input v-model="ruleForm.description" placeholder="请输入图书详情" maxlength="50"/>
            </el-form-item>
            <el-form-item label="借书时间">
              <el-date-picker
                      v-model="ruleForm.startTime"
                      type="date"
                      placeholder="选择日期">
              </el-date-picker>
            </el-form-item>
            <el-form-item label="还书时间">
              <el-date-picker
                      v-model="ruleForm.endTime"
                      type="date"
                      placeholder="选择日期">
              </el-date-picker>
            </el-form-item>
          </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="submit()">确 定</el-button>
        </div>
      </el-dialog>
      <el-pagination
        class="pageList"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="counts"
        :current-page.sync="page"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      ></el-pagination>
    </div>
  </div>
    <!-- 开发环境版本，包含了有帮助的命令行警告 -->
    <script src="../../plugins/vue/vue.js"></script>
    <!-- 引入组件库 -->
    <script src="../../plugins/element-ui/index.js"></script>
    <!-- 引入axios -->
    <script src="../../plugins/axios/axios.min.js"></script>
    <script src="../../js/request.js"></script>
    <script src="../../api/book.js"></script>
    <script>
      new Vue({
        el: '#member-app',
        data() {
          return {
             input: '',
             counts: 0,
             page: 1,
             pageSize: 10,
             tableData : [],
             id : '',
             status : '',
             checkList: [],
             dialogFormVisible: false,
            ruleForm: {
              name: '',
              author: '',
              price: '',
              typeId: '',
              description: '',
              startTime: '',
              endTime: '',
              type: ''
            },
            formLabelWidth: '120px'
          }
        },

        computed: {},
        created() {
          this.init()
          this.user = JSON.parse(localStorage.getItem('userInfo')).username
        },
        mounted() {
        },
        methods: {
          submit() {
            console.log('submit!');
            const params = {
              ...this.ruleForm,
            }
            console.log("params",params)
            LibraryBook(params).then((res) => {
              if (res.code === 1) {
                this.$message.success(res.data)
                this.dialogFormVisible = false;
                this.handleQuery()
              } else {
                this.$message.error(res.msg || '操作失败')
              }
            }).catch(err => {
              this.$message.error('请求出错了：' + err)
            })
          },
          async init () {
            const params = {
              page: this.page,
              pageSize: this.pageSize,
              name: this.input ? this.input : undefined
            }
            await getHotBookList(params).then(res => {
              if (String(res.code) === '1') {
                this.tableData = res.data.records || []
                this.counts = res.data.total
              }
            }).catch(err => {
              this.$message.error('请求出错了：' + err)
            })
          },
          notHotBookHandle(id){
            this.$confirm('确认取消推荐该图书, 是否继续?', '确定取消推荐', {
              'confirmButtonText': '确定',
              'cancelButtonText': '取消',
            }).then(() => {
              notHotBook(id).then(res => {
                if (res.code === 1) {
                  this.$message.success('取消推荐成功！')
                  this.handleQuery()
                } else {
                  this.$message.error(res.msg || '操作失败')
                }
              }).catch(err => {
                this.$message.error('请求出错了：' + err)
              })
            })
          },
          handleQuery() {
            this.page = 1;
            this.init();
          },
           // 添加
          addBookHandle (st) {
            if (st === 'add'){
              window.parent.menuHandle({
                id: '2',
                url: '/backend/page/info/add.html?type='+st,
                name: '添加图书'
              },true)
            } else {
              window.parent.menuHandle({
                id: '2',
                url: '/backend/page/info/add.html?id='+st,
                name: '修改图书'
              },true)
            }
          },
          //删除
          deleteBookHandle(type,id) {
            if (type === '批量' && id === null) {
              if (this.checkList.length === 0) {
                return this.$message.error('请选择删除对象')
              }
            }
            this.$confirm('确认删除该图书, 是否继续?', '确定删除', {
              'confirmButtonText': '确定',
              'cancelButtonText': '取消',
            }).then(() => {
              deleBook(type === '批量' ? this.checkList.join(',') : id).then(res => {
                if (res.code === 1) {
                  this.$message.success('删除成功！')
                  this.handleQuery()
                } else {
                  this.$message.error(res.msg || '操作失败')
                }
              }).catch(err => {
                this.$message.error('请求出错了：' + err)
              })
            })
          },
          LibraryBookHandle(id){
            this.$confirm('确认借阅该图书, 是否继续?', '确定借阅', {
              'confirmButtonText': '确定',
              'cancelButtonText': '取消',
            }).then(() => {
              queryBookById(id).then(res => {
                console.log(res)
                if (String(res.code) === '1') {
                  console.log(res.data)
                  this.ruleForm = res.data
                  // this.ruleForm.password = ''
                } else {
                  this.$message.error(res.msg || '操作失败')
                }
              })
              this.dialogFormVisible = true

            })
          },
          getImage (image) {
            return `/common/download?name=${image}`
          },
          // 全部操作
          handleSelectionChange (val){
            let checkArr = []
            val.forEach((n) => {
              checkArr.push(n.id)
            })
            this.checkList = checkArr
          },
          handleSizeChange (val) {
            this.pageSize = val
            this.init()
          },
          handleCurrentChange (val) {
            this.page = val
            this.init()
          },


        }
      })
    </script>
</body>
</html>